package wyq.gy.fragment;


import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.SaveCallback;
import wyq.gy.Bean.robOrderMsg;
import wyq.gy.R;
import wyq.gy.lib.Constants;

import static wyq.gy.lib.Constants.isDialogShow;

public class InfoDialogFragment extends DialogFragment {
    private static String TAG = "InfoDialogFragment";
    robOrderMsg mMsg = new robOrderMsg();

    public InfoDialogFragment() {
    }

    public void SetData(robOrderMsg busi) {
        mMsg = busi;
    }

    @Override
    public void onStart() {
        super.onStart();
        // 设置dialog的layout
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();
        layoutParams.width = dm.widthPixels;
        layoutParams.height = layoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.CENTER;
        getDialog().getWindow().setAttributes(layoutParams);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_info_dialog, container, false);
        TextView nameTv = (TextView)view.findViewById(R.id.worker_name);
        if(mMsg.robOrderName != null) {
            nameTv.setText(mMsg.robOrderName);
        }
        TextView orderidTv = (TextView)view.findViewById(R.id.order_id);
        if(mMsg.orderID != null) {
            orderidTv.setText(mMsg.orderID);
        }
        TextView phoneTV = (TextView)view.findViewById(R.id.rob_phone);
        if(mMsg.robWokerPhone != null) {
            phoneTV.setText(mMsg.robWokerPhone);
        }
        TextView birthTV = (TextView)view.findViewById(R.id.rob_birth);
        if(mMsg.robBirth != null) {
            birthTV.setText(mMsg.robBirth);
        }
        TextView starTV = (TextView)view.findViewById(R.id.star);
        if(mMsg.star != null) {
            starTV.setText(mMsg.star);
        }
        Button acceptBtn = (Button)view.findViewById(R.id.accept_btn);
        acceptBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AVObject todo = AVObject.createWithoutData("RobMyOrderList", Constants.MyOrderObjectIdG);
                todo.put("State", "2");
                Log.e(TAG, "同意" + Constants.MyOrderObjectIdG);
                todo.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                       if(e == null){
                           dismiss();
                           Constants.MyOrderObjectIdG=null;
                           isDialogShow = false;
                           Toast.makeText(getActivity(), "同意成功", Toast.LENGTH_SHORT).show();
                       }else {
                           Toast.makeText(getActivity(), "更新失败" + e.toString(), Toast.LENGTH_SHORT).show();
                       }
                    }
                });
            }
        });
        Button forbidBtn = (Button)view.findViewById(R.id.forbid_btn);
        forbidBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AVObject todo = AVObject.createWithoutData("RobMyOrderList", Constants.MyOrderObjectIdG);
                todo.put("State", "4");//拒绝工人请求
                todo.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                        if(e == null){
                            dismiss();
                            Constants.MyOrderObjectIdG=null;
                            isDialogShow = false;
                            Toast.makeText(getActivity(), "拒绝成功", Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(getActivity(), "更新失败" + e.toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });

        return view;
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);//设置actionbar的隐藏
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }
}
